import QtQuick 2.6
import Sailfish.Silica 1.0
import "../config"

Rectangle {
    width: parent.width
    height: 100
    color:AppColor.themeMode.transparent

    property int processHeight: height - 2 * verticalPadding   //进度条高度
    property int processWidth: width - 2 * horizontalPadding   //进度条宽度
    property int processRadius: 0
    property int horizontalPadding: 30                  //组件内部水平padding
    property int verticalPadding: 30                    //组件内部垂直padding

    property real maxValue: 100                         // 进度最大值
    property real minValue: 0                           // 进度最小值
    property real progress: 0.5                         // 初始进度，0到1之间


    // 背景条
    Rectangle {
        id: backgroundBar
        width: processWidth
        height: processHeight
        radius: processRadius===0?(Math.min(width,height)/2):processRadius // 圆角大小
        anchors.centerIn: parent
        color: AppColor.themeMode.secondaryComponentColor
        clip: true
    }

    // 进度条
    Rectangle {
        id: progressBar
        width: backgroundBar.width * Math.min(progress, 1)
        height: (width < backgroundBar.radius)? (width/backgroundBar.radius)*backgroundBar.height : backgroundBar.height
        radius: (width < backgroundBar.radius)? width / 2 : backgroundBar.radius
        color: AppColor.themeMode.thirdColor
        anchors.verticalCenter: parent.verticalCenter
        anchors.left: backgroundBar.left
        Behavior on x { PropertyAnimation { duration: 200 } }
    }

    // 滑动区域
    MouseArea {
        id: sliderMouseArea
        anchors.fill: parent
        hoverEnabled: true

        property bool release: false

        onPressed: {
            release = false;
        }

        onReleased: {
            release = true;
        }

        onPositionChanged: {
            if(release)
                return;     //释放时，mouse.x会十分巨大，故此

            // 计算相对于背景条的鼠标位置
            var relativeX = mouse.x - backgroundBar.x;

            // 根据拖动位置更新进度
            if(relativeX > backgroundBar.width)
                progress = 1;
            else if(relativeX < 0)
                progress = 0;
            else
                progress = relativeX / backgroundBar.width;
        }
    }
}
